<template>
  <div class='container'>
    <el-dialog :title="titleText" :visible="dialogVisible" width="800px" @close="onClose">
      <el-form ref="addForm" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="文章标题" prop="title">
          <el-input v-model.trim="form.title" placeholder="请输入文章标题"></el-input>
        </el-form-item>
        <el-form-item label="文章内容" prop="articleBody">
          <!--          富文本-->
          <quill-editor
            ref="myQuillEditor"
            v-model="form.articleBody"
            :options="editorOption"
            @blur="quillBlur"
          >
          </quill-editor>
        </el-form-item>
        <el-form-item label="视频地址">
          <el-input v-model.trim="form.videoURL" placeholder="请输入视频地址"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="onClose">取 消</el-button>
    <el-button type="primary" @click="onSave">确 定</el-button>
  </span>

    </el-dialog>
  </div>
</template>

<script>
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
import 'highlight.js/styles/monokai-sublime.css' // 代码高亮样式
import hljs from 'highlight.js' // 代码高亮
import { quillEditor } from 'vue-quill-editor'
import { add, detail, update } from '../../api/hmmm/articles.js'

export default {
  name: 'articles-add',
  props: {
    dialogVisible: {
      type: Boolean,
      required: true
    },
    id: {
      type: [Number, String],
      required: true
    }
  },
  data () {
    return {
      form: {
        title: '',
        articleBody: '', // 文章内容
        videoURL: '' // 视频地址
      },
      // 富文本数据
      editorOption: {
        placeholder: '',
        modules: {
          toolbar: [
            ['bold', 'italic', 'underline', 'strike'], // 加粗，斜体，下划线，删除线
            [{ list: 'ordered' }, { list: 'bullet' }], // 列表
            ['blockquote'], // 引用，
            ['code-block', 'image', 'link'] // 代码块,上传图片、上传视频
          ],
          syntax: { // 代码高亮
            highlight: text => {
              return hljs.highlightAuto(text).value // 这里就是代码高亮需要配置的地方
            }
          }
        }
      },
      rules: {
        title: [{
          required: true,
          message: '请输入文章标题',
          trigger: 'blur'
        }],
        articleBody: [{
          required: true,
          message: '请输入文章内容',
          trigger: 'blur'
        }]
      }
    }
  },
  components: {
    quillEditor
  },
  computed: {
    titleText () {
      return this.form.id ? '修改文章' : '新增文章'
    }
  },
  methods: {
    // 点击确定
    async onSave () {
      await this.$refs.addForm.validate(async (valid) => {
        if (valid) {
          if (!this.form.id) { // 创建
            // 发起添加用户请求
            console.log(this.form)
            await add(this.form)
            this.$message.success('添加成功')
          } else { // 编辑
            await update(this.form)
            await this.$message.success('修改成功')
          }
          await this.onClose()
          this.$emit('successAdd')
        } else {
          return false
        }
      })
    },
    // 关闭
    async onClose () {
      await this.$refs.addForm.resetFields()
      this.$emit('update:dialogVisible', false)
      this.form = {
        title: '',
        articleBody: '', // 文章内容
        videoURL: '' // 视频地址
      }
    },

    // 通过id获取到编辑的数据
    async loadEditRowById () {
      const { data } = await detail({
        id: this.id
      })
      this.form = data
    },
    quillBlur () {
      this.$refs.addForm.validateField('articleBody')
    }
  }
}
</script>

<style lang='less' scoped>
/deep/ .ql-editor {
  height: 200px;
}
</style>
